package com.yztc.ui.classify;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yztc.bean.classify.ClassifyDataBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.classify.myadapter.GridAdapter;
import com.yztc.ui.classifydetail.ClassifyDetailActivity;
import com.yztc.utils.ImageHelper;

import java.util.List;

/**
 * Created by My on 2016/10/6.
 */
public class ClassifyFragment extends Fragment implements ClassifyContract.View{
    //presenter对象
    private ClassifyPresenter presenter;
    //布局控件
    private View view;
    private ImageButton ib_location_classify;
    private TextView tv_location_classify;
    private EditText et_classify;
    private ImageButton ib_vertical_classify;
    private XRecyclerView rv_classify;
    private View footView;
    private ImageView iv_classify_foot;
    //适配器
    private GridAdapter adapter;
    //GridView图片集合
    List<ClassifyDataBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_classify,null);
        footView=inflater.inflate(R.layout.item_classify_foot,null);

        //初始化控件
        initView();

        presenter=new ClassifyPresenter(this);
        presenter.getVerticalClassifyDataBean(getContext());
        

        return view;
    }

    //定义接口定义RecyclerView中的item的点击事件
    public interface onItemClickListener{
        public void onItemClick(View view,int position);
        public void onItemLongClick(View view,int position);
    }

    private void initView() {
        ib_location_classify= (ImageButton) view.findViewById(R.id.ib_location_classify);
        tv_location_classify= (TextView) view.findViewById(R.id.tv_location_classify);
        et_classify= (EditText) view.findViewById(R.id.et_classify);
        ib_vertical_classify= (ImageButton) view.findViewById(R.id.ib_vertical_classify);
        rv_classify= (XRecyclerView) view.findViewById(R.id.rv_classify);
        iv_classify_foot= (ImageView) footView.findViewById(R.id.iv_classify_foot);
    }

    private void setRecyclerView() {
        rv_classify.setLayoutManager(new GridLayoutManager(getContext(),2));
        rv_classify.setItemAnimator(new DefaultItemAnimator());
//        rv_classify.addItemDecoration(new DividerGridItemDecoration(getContext()));
        adapter=new GridAdapter(getContext(),list);

        //设置监听事件
        adapter.setOnItemClickListener(new onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getContext(), ClassifyDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("category_id",list.get(position).getCategory_id());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        ImageHelper.getNormalImage(getContext(),list.get(list.size()-1).getIcon_url(),iv_classify_foot);
        iv_classify_foot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), ClassifyDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("category_id",list.get(list.size()-1).getCategory_id());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        rv_classify.addFootView(footView);
        rv_classify.setAdapter(adapter);
    }

    //view的回调函数，得到适配器所用集合
    @Override
    public void ongetVerticalClassifyDataBean(List<ClassifyDataBean> classifyDataBeen) {
        list=classifyDataBeen;
        //设置RecyclerView
        setRecyclerView();
    }

    //View的回调函数，加载数据错误弹出错误信息
    @Override
    public void onGetVerticalFailed(Throwable error) {
        Toast.makeText(getContext(),error.toString(), Toast.LENGTH_SHORT).show();
    }
}
